<template>
  <div class="control">
    <el-button-group>
      <el-button type="primary" :icon="ZoomIn" title="放大" @click="zoomIn" />
      <el-button type="primary" :icon="ZoomOut" title="缩小" @click="zoomOut" />
      <el-button type="primary" :icon="FullScreen" title="大小适应" @click="zoomReset" />
      <el-button type="primary" :icon="Aim" title="定位还原" @click="translateReset" />
      <el-button type="primary" :icon="Back" title="上一步" :disabled="undoDisable" @click="undo" />
      <el-button
        type="primary"
        :icon="Right"
        title="下一步"
        :disabled="redoDisable"
        @click="redo"
      />
      <el-button type="primary" :icon="Download" title="新建指标" @click="createIncator" />
    </el-button-group>
  </div>
</template>
<script lang="ts">
export default { name: "Control" };
</script>
<script setup lang="ts">
import { ZoomIn, ZoomOut, FullScreen, Aim, Back, Right, Download } from "@element-plus/icons-vue";
import LogicFlow from "@logicflow/core";
import { TableListParameter } from "@/api/strategy/strategy-index-database";
const router = useRouter();
const emit = defineEmits(["showIndicatorOutput"]);
const props = defineProps<{
  lf?: LogicFlow;
  parameter: TableListParameter;
  dataSourceName: string;
}>();
const undoDisable = ref(true);
const redoDisable = ref(true);

const zoomIn = () => {
  props.lf?.zoom(true);
};

const zoomOut = () => {
  props.lf?.zoom(false);
};

const zoomReset = () => {
  props.lf?.resetZoom();
};

const translateReset = () => {
  props.lf?.translateCenter();
};

const undo = () => {
  props.lf?.undo();
};

const redo = () => {
  props.lf?.redo();
};

const createIncator = () => {
  if (props.lf) {
    const graphModel = props.lf.graphModel;
    const indicatorOutputNodes = graphModel.nodes.filter((node) => {
      return (node.type as string) === "indicator_output";
    });
    let allIndicatorOutputData: any[] = [];
    let newTableName = "";

    if (indicatorOutputNodes.length >= 1) {
      indicatorOutputNodes.forEach((node) => {
        const properties = node.getProperties();
        console.log(properties.newTableName);
        const data = properties.indicatorOutputData || [];
        newTableName = properties.newTableName;
        allIndicatorOutputData = allIndicatorOutputData.concat(data);
      });
    }
    const dataToSend = {
      dataSourceId: props.parameter.id,
      dataSourceName: props.dataSourceName,
      newTableName: newTableName,
      columns: allIndicatorOutputData,
    };
    console.log("被触发");
    emit("showIndicatorOutput", dataToSend);
    // router.push({
    //   name: "indicatorOutput",
    //   query: {
    //     indicatorOutputData: JSON.stringify(dataToSend),
    //   },
    // });
  }
};

onMounted(() => {
  props.lf &&
    props.lf.on("history:change", ({ data }) => {
      undoDisable.value = !data.undoAble;
      redoDisable.value = !data.redoAble;
    });
});
</script>
<style lang="scss" scoped>
.control {
  position: absolute;
  bottom: 0;
  left: 150px;
}
</style>
